/**
 * 删除文件
 * @param {btn} btn
 */
function deleteFile(btn) {
    restFileOptDialog();
    var path = getCurrentFileDialogPath();

    $.post("/api/res/delete", {"file": path}, function (result) {
        showFileOptDialog(false)
        if (result.code < 0) {
            showMessageDialog(true, result.msg)
            return;
        }
        $(".file-window-body ul").children().each(function (i, n) {
            if ($(this).data("file-data").absPath == path) {
                $(this).remove();
            }
        });

    })
}

/**
 * 加载指定目录下文件
 * @param {}} parentRoot
 */
function loadFile(parentRoot) {
    showLoading(true)
    $.get("/api/file/listFile", {"parentPath": parentRoot},
        function (result) {
            showLoading(false)
            if (result.code == 0) {
                /**
                 * 渲染
                 */
                renderingList(result.data)
            } else {
                showMessageDialog(true, result.msg)
                removeNavLastOne();
            }
        });
}

/**
 * 标记文件
 * @param {元素，通过元素属性获取收藏的路径} els
 * @param {是否收藏} mark
 */
function markfile(els, mark) {
    path = $(els).attr("data-path")

    $.ajax({
        url: "/api/file/mark",
        type: "POST",
        data: {"file": path, "mark": mark},
        success: function (result) {
            showMessageDialog(true, result.msg);
            if (!mark && result.code == 0) {
                $(".file-window-body ul").children().each(function (i, n) {
                    if ($(this).data("file-data").absPath == path) {
                        $(this).remove();
                    }
                });

            }
        }
    });
}


/**
 * 加载收藏文件
 */
function loadMarkFile() {
    showLoading(true)
    $.get("/api/file/listMark",
        function (result) {
            showLoading(false)
            if (result.code == 0) {
                renderingList(result.data)
            } else {
                showMessageDialog(true, result.msg)
                removeNavLastOne();
            }
        });
}

function startDownLoad() {
    var a = document.createElement('a');
    var path = $(".dialog-file-opt").attr("data-path")
    a.href = ("/api/res/get?path=" + path);
    a.click()
    showFileOptDialog(false)
}

/**
 * 文件上传
 * @param {文件} file
 */
function uploadItemFile(file) {
    var formData = new FormData();
    formData.append('upfile', file);
    formData.append("path", getCurrentWhere())
    showLoading(true);
    $.ajax({
        url: "/api/res/upload",
        type: "POST",
        data: formData,
        contentType: false,
        processData: false,
        xhr: function () {
            var xhr = new XMLHttpRequest();
            //使用XMLHttpRequest.upload监听上传过程，注册progress事件，打印回调函数中的event事件
            xhr.upload.addEventListener('progress', function (e) {
                var progressRate = (e.loaded / e.total) * 100;
                $(".loading .progress").html(Math.ceil(progressRate) + "%");
            })

            return xhr;
        },
        success: function (result) {
            $(".loading .progress").html("");
            showLoading(false);
            if (result.code < 0) {
                showMessageDialog(true, result.msg);
            } else {
                loadFile(getCurrentWhere())
            }
        }
    });
}

function editFile() {
    var fileData = $(".dialog-file-opt").data("file-data");
    showLoading(true)
    $.post("/api/res/getFileContent", {"path": fileData.absPath}, function (result) {
        showLoading(false)
        if (result.code < 0) {
            showMessageDialog(true, result.msg)
            return;
        }
        showEditDialog(true)
        $(".dialog-edit header").html(fileData.absPath)

        $(".dialog-edit textarea").val(result.data)
    })
}

function setFileContent() {
    var fileData = $(".dialog-file-opt").data("file-data");
    var currentContent = $(".dialog-edit textarea").val()
    showLoading(true);
    $.post("/api/res/setFileContent", {"path": fileData.absPath,"content":currentContent}, function (result) {
        showEditDialog(false)
        showMessageDialog(true, result.msg)

    })
}